package cn.rokhdelar.ams.business.biz.projectbaseflow.controller;

import cn.rokhdelar.ams.business.biz.projectbaseflow.domain.AuditRecord;
import cn.rokhdelar.ams.business.biz.projectbaseflow.service.AuditRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 审核记录API控制器
 */
@RestController
@RequestMapping("/api/audit-records")
@Tag(name = "审核记录管理", description = "审核记录的查询、新增等操作")
public class AuditRecordController {

    @Autowired
    private AuditRecordService auditRecordService;

    @Operation(summary = "根据流程实例ID查询审核记录", 
            description = "获取指定流程实例的所有审核记录，按审核时间倒序排列",
            responses = {
                @ApiResponse(responseCode = "200", description = "查询成功",
                        content = @Content(schema = @Schema(implementation = AuditRecord.class))),
                @ApiResponse(responseCode = "404", description = "未找到相关记录")
            })
    @GetMapping("/process/{processInstanceId}")
    public List<AuditRecord> getByProcessInstanceId(
            @Parameter(name = "processInstanceId", description = "流程实例ID", 
                    in = ParameterIn.PATH, required = true, example = "proc123456")
            @PathVariable String processInstanceId) {
        return auditRecordService.getByProcessInstanceId(processInstanceId);
    }

    @Operation(summary = "根据任务ID查询审核记录",
            description = "获取指定任务实例的审核记录详情",
            responses = {
                @ApiResponse(responseCode = "200", description = "查询成功"),
                @ApiResponse(responseCode = "404", description = "未找到相关记录")
            })
    @GetMapping("/task/{taskId}")
    public AuditRecord getByTaskId(
            @Parameter(name = "taskId", description = "任务实例ID",
                    in = ParameterIn.PATH, required = true, example = "task7890")
            @PathVariable String taskId) {
        return auditRecordService.getByTaskId(taskId);
    }

    @Operation(summary = "新增审核记录",
            description = "手动添加审核记录（通常由系统自动调用）",
            responses = {
                @ApiResponse(responseCode = "200", description = "保存成功"),
                @ApiResponse(responseCode = "400", description = "参数错误")
            })
    @PostMapping
    public boolean saveAuditRecord(
            @Parameter(description = "审核记录信息", required = true)
            @RequestBody AuditRecord auditRecord) {
        return auditRecordService.save(auditRecord);
    }
}
    